/*
 *File: BlockGroup.cpp
 *Authors: Diane, Dan, and Tim
 *Created Oct 2012
 */
#include "BlockGroup.h"
#include "BlockLinkedList.h"
#include <fnt1.h>

BlockGroup::BlockGroup(BlockLinkedList* bll) {
	m_disk = bll.getDisk();
	m_numberOfBlocks = 0;
	m_blockSize = bll.getBlockSize();
}

void BlockGroup::SetFreeList(BlockLinkedList* motherFreeList) {
	if(motherFreeList != NULL){
		m_disk = motherFreeList->getDisk();
		numberOfBlocks = 0;
		m_blockSize = motherFreeList->getBlockSize();
		m_startBlockNumber = 0;
		m_endBlockNumber = 0;
		m_currentBlockNumber = startBlockNumber;
}

BlockGroup::BlockGroup(unsigned int startBlock, unsigned int 
		endBlock, unsigned int numberOfBlocks, 
		BlockLinkedList *motherFreeList) {

	this->m_blockSize = m_disk->blockSize() -4;
	this->m_numberOfBlocks = numberOfBlocks;
	this->m_startBlockNumber = startBlock;
	this->m_endBlockNumber = endBlock;
	this->m_currentBlockNumber = startBlockNumber;

}


bool BlockGroup::AddBlock(){
	if(this->motherFreeList->getNumberOfBlocks() > 0) {
		if(numbterOfBlocks ==0) { //If empty, initialize with firstBlock from FreeList.
			FSBlock* firstBlock = motherFreeList->UnlinkBlock();
			int startNumber = firstBlock->getBlockNumber();
			delete firstBlock;
			return initialize(startNumber);
		}
		else if{
			FSBlock* currentBlock = motherFreeList->UnlinkBlock();
			bool addBlockStatus = BlockLinkedList::addBlock(currentBlock);
			delete currentBlock;
			return addBlockStatus;
		}
		else {
			printf("BlockGroup addBlock Failed.");
			return false;
		}
	}


}




